<template>
  <el-form
    ref="form"
    :label-position="device === 'mobile' ? 'top' : 'right'"
    label-width="100px"
    :model="form"
    :rules="rules"
  >
    <el-form-item label="名称" prop="name">
      <el-input
        v-model.trim="form.name"
        autocomplete="off"
        placeholder="请输入名称"
      />
    </el-form-item>
    <el-form-item label="种类" prop="type">
      <el-input
        v-model.trim="form.type"
        autocomplete="off"
        placeholder="请输入种类"
      />
    </el-form-item>
    <el-form-item label="数量" prop="num">
      <el-input
        v-model.trim="form.num"
        autocomplete="off"
        placeholder="请输入数量"
      />
    </el-form-item>
    <el-form-item label="单位" prop="unit">
      <el-input
        v-model.trim="form.unit"
        autocomplete="off"
        placeholder="请输入单位"
      />
    </el-form-item>
    <el-form-item label="处理方式" prop="processingMode">
      <el-input
        v-model.trim="form.processingMode"
        autocomplete="off"
        placeholder="请输入处理方式"
      />
    </el-form-item>
    <el-form-item label="经办人员" prop="worker">
      <el-input
        v-model.trim="form.worker"
        autocomplete="off"
        placeholder="请输入经办人员"
      />
    </el-form-item>
    <el-form-item label="效果" prop="effect">
      <el-input
        v-model.trim="form.effect"
        autocomplete="off"
        placeholder="请输入效果"
      />
    </el-form-item>
    <el-form-item v-if="device === 'mobile'" class="submit-content">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from "vuex";
import { API_garbageCreate, API_garbageEdit } from "@/api/garbage";
export default {
  name: "FormEdit",
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        name: "",
        type: "",
        num: "",
        unit: "",
        processingMode: "",
        worker: "",
        effect: "",
      },
      rules: {
        name: [{ required: true, trigger: "blur", message: "请输入名称" }],
        type: [{ required: true, trigger: "blur", message: "请输入种类" }],
        num: [{ required: true, trigger: "blur", message: "请输入数量" }],
        unit: [{ required: true, trigger: "blur", message: "请输入单位" }],
        processingMode: [
          { required: true, trigger: "blur", message: "请输入处理方式" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      device: "settings/device",
    }),
  },
  watch: {
    formData: {
      handler(newData) {
        if (newData) {
          this.form = Object.assign({}, this.formData);
        }
      },
      immediate: true,
    },
  },
  methods: {
    close() {
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
      if (this.device === "mobile") {
        this.$router.push({
          name: "Garbage",
          params: { refresh: true },
        });
      }
    },
    save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (this.form.code) {
            const { message } = await API_garbageEdit(this.form);
            this.$baseMessage(message, "success");
          } else {
            const { message } = await API_garbageCreate(this.form);
            this.$baseMessage(message, "success");
          }
          this.$emit("fetch-data");
          this.close();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
